/*
	Copyright (c) 2004-2006, The Dojo Foundation
	All Rights Reserved.

	Licensed under the Academic Free License version 2.1 or above OR the
	modified BSD license. For more information on Dojo licensing, see:

		http://dojotoolkit.org/community/licensing.shtml
*/

//
// User defined button widget
// that extends dojo's button widget by setting custom images
//
// In java terminology, this file defines
// a class called acme.widget.Button that extends dojo.widget.Button
//
dojo.provide("openbravo.widget.DateTextbox");
dojo.require("dojo.widget.DateTextbox");
dojo.require("openbravo.messages.common");

// <namespace>, <namespace>.widget is now considered 'conventional'
// therefore the registerNamespace call below is no longer necessary here

// Tell dojo that widgets prefixed with "openbravo:" namespace are found in the "acme.widget" module
//dojo.registerNamespace("acme", "acme.widget");

// define UserButton's constructor
dojo.widget.defineWidget(
	// class
	"openbravo.widget.DateTextbox",

	// superclass	
	dojo.widget.DateTextbox,
	
	// member variables/functions
	{
		// override displayFormat: String
		//	optional pattern used format date.  Uses locale-specific format by default.  See dojo.date.format.
		displayFormat: "",
		// override saveFormat: String
		//	optional pattern used format date.  Uses locale-specific format by default.  See dojo.date.format.
		saveFormat: "",
		listenOnKeyPress:false,
		// override css
		templatePath: dojo.uri.dojoUri("../openbravo/widget/templates/DateTextbox.html"),
		templateCssPath: null, // Defined in the skin --- ValidationTextbox.css

		postMixInProperties:function(_b04,frag){
		  openbravo.widget.DateTextbox.superclass.postMixInProperties.apply(this,arguments);
		  //this.value=this.dateToRfc3339(this.value,this.displayFormat);
    this.invalidMessage= openbravo.messages.getDataBaseMessage("Invalid");
    this.missingMessage= openbravo.messages.getDataBaseMessage("Missing");
    this.rangeMessage= openbravo.messages.getDataBaseMessage("Range");
    this.messages.invalidMessage= openbravo.messages.getDataBaseMessage("Invalid");
    this.messages.missingMessage= openbravo.messages.getDataBaseMessage("Missing");
    this.messages.rangeMessage= openbravo.messages.getDataBaseMessage("Range");
		},

		fillInTemplate:function(args,frag){
		  this.displayFormat=this.displayFormat.replace("mm","MM").replace("DD","dd").replace("YYYY","yyyy");
		  this.saveFormat=this.displayFormat;
		  openbravo.widget.DateTextbox.superclass.fillInTemplate.apply(this,arguments);
      this.invalidSpan.style.display="none";
      this.missingSpan.style.display="none";
      this.rangeSpan.style.display="none";
		},

    isValid: function(){ 
      str_datetime = this.textbox.value;
      str_dateFormat = this.displayFormat;
      if (str_datetime.length == 0) return true;
      // datetime parsing and formatting routimes. modify them if you wish other datetime format
    //function str2dt (str_datetime) {
      var re_date = /^(\d+)[\-|\/|\.](\d+)[\-|\/|\.](\d+)$/;
      if (!re_date.exec(str_datetime))
        return false;
      if (!str_dateFormat) str_dateFormat = defaultDateFormat;
      switch (str_dateFormat) {
        case "MM\/DD\/YYYY":
        case "MM\/DD\/YY":
        case "MM-DD-YYYY":
        case "MM-DD-YY":
        case "MM.DD.YYYY":
        case "MM.DD.YY":
        case "%m-%d-%Y":
          if (RegExp.$2 < 1 || RegExp.$2 > 31) return false;
          if (RegExp.$1 < 1 || RegExp.$1 > 12) return false;
          if (RegExp.$3 < 1 || RegExp.$3 > 9999) return false;
        case "YYYY\/MM\/DD":
        case "YY\/MM\/DD":
        case "YYYY-MM-DD":
        case "YY-MM-DD":
        case "YYYY.MM.DD":
        case "YY.MM.DD":
        case "%Y-%m-%d":
          if (RegExp.$3 < 1 || RegExp.$3 > 31) return false;
          if (RegExp.$2 < 1 || RegExp.$2 > 12) return false;
          if (RegExp.$1 < 1 || RegExp.$1 > 9999) return false;
        case "DD\/MM\/YYYY":
        case "DD\/MM\/YY":
        case "DD-MM-YYYY":
        case "DD-MM-YY":
        case "DD.MM.YYYY":
        case "DD.MM.YY":
        case "%d-%m-%Y":
        default:
          if (RegExp.$1 < 1 || RegExp.$1 > 31) return false;
          if (RegExp.$2 < 1 || RegExp.$2 > 12) return false;
          if (RegExp.$3 < 1 || RegExp.$3 > 9999) return false;
      }
      return (new Date (RegExp.$1, RegExp.$2, RegExp.$3, 0, 0, 0));
    },
		dateToRfc3339: function(/*String*/dateObject, /*String*/formattedString){
		  // summary: sets a Date object based on an ISO 8601 formatted string (date only)
		  var year = null;
		  var month = null;
		  var day = null;

		  switch (formattedString.toUpperCase())
		  {
				case "DD-MM-YYYY":
				case "DD/MM/YYYY":
						  year = dateObject.substring(6,10);
						  month = dateObject.substring(3,5);
						  day = dateObject.substring(0,2);
						  dateObject = year+"-"+month+"-"+day;
				break;
				case "MM-DD-YYYY":
				case "MM/DD/YYYY":
						  year = dateObject.substring(6,10);
						  month = dateObject.substring(0,2);
						  day = dateObject.substring(3,5);
						  dateObject = year+"-"+month+"-"+day;
				break;
		  }
		  return dateObject; // Date
		}
	}
);